package game

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/game"
    gameReq "github.com/flipped-aurora/gin-vue-admin/server/model/game/request"
)

type GameServerService struct {}

// CreateGameServer 创建gameServer表记录
// Author [piexlmax](https://github.com/piexlmax)
func (gameServerService *GameServerService) CreateGameServer(gameServer *game.GameServer) (err error) {
	err = global.GVA_DB.Create(gameServer).Error
	return err
}

// DeleteGameServer 删除gameServer表记录
// Author [piexlmax](https://github.com/piexlmax)
func (gameServerService *GameServerService)DeleteGameServer(ID string) (err error) {
	err = global.GVA_DB.Delete(&game.GameServer{},"id = ?",ID).Error
	return err
}

// DeleteGameServerByIds 批量删除gameServer表记录
// Author [piexlmax](https://github.com/piexlmax)
func (gameServerService *GameServerService)DeleteGameServerByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]game.GameServer{},"id in ?",IDs).Error
	return err
}

// UpdateGameServer 更新gameServer表记录
// Author [piexlmax](https://github.com/piexlmax)
func (gameServerService *GameServerService)UpdateGameServer(gameServer game.GameServer) (err error) {
	err = global.GVA_DB.Model(&game.GameServer{}).Where("id = ?",gameServer.ID).Updates(&gameServer).Error
	return err
}

// GetGameServer 根据ID获取gameServer表记录
// Author [piexlmax](https://github.com/piexlmax)
func (gameServerService *GameServerService)GetGameServer(ID string) (gameServer game.GameServer, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&gameServer).Error
	return
}

// GetGameServerInfoList 分页获取gameServer表记录
// Author [piexlmax](https://github.com/piexlmax)
func (gameServerService *GameServerService)GetGameServerInfoList(info gameReq.GameServerSearch) (list []game.GameServer, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&game.GameServer{})
    var gameServers []game.GameServer
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }
	
	err = db.Find(&gameServers).Error
	return  gameServers, total, err
}